home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / EXAMPLES / OHIDDEN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  6.1 KB  |  264 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /**
  5.  * (c) Copyright 1993, 1994, Silicon Graphics, Inc.
  6.  * ALL RIGHTS RESERVED 
  7.  * Permission to use, copy, modify, and distribute this software for 
  8.  * any purpose and without fee is hereby granted, provided that the above
  9.  * copyright notice appear in all copies and that both the copyright notice
  10.  * and this permission notice appear in supporting documentation, and that 
  11.  * the name of Silicon Graphics, Inc. not be used in advertising
  12.  * or publicity pertaining to distribution of the software without specific,
  13.  * written prior permission. 
  14.  *
  15.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  16.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  17.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  18.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  19.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  20.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  21.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  22.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  23.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  24.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  25.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  26.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  27.  * 
  28.  * US Government Users Restricted Rights 
  29.  * Use, duplication, or disclosure by the Government is subject to
  30.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  31.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  32.  * clause at DFARS 252.227-7013 and/or in similar or successor
  33.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  34.  * Unpublished-- rights reserved under the copyright laws of the
  35.  * United States.  Contractor/manufacturer is Silicon Graphics,
  36.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  37.  *
  38.  * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
  39.  */
  40. /*----------------------------------------------------------------------------
  41.  *
  42.  * ohidden.c : openGL (GLUT) example showing how to use stencils
  43.  *             to draw outlined polygons.
  44.  *
  45.  * Author : Yusuf Attarwala
  46.  *          SGI - Applications
  47.  * Date   : Jul 93
  48.  *
  49.  * Update : Mark Kilgard
  50.  * Date   : Feb 95
  51.  *
  52.  *    note : the main intent of this program is to demo the stencil
  53.  *           plane functionality, hence the rendering is kept
  54.  *           simple (wireframe).
  55.  *
  56.  *---------------------------------------------------------------------------*/
  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <string.h>
  60.  
  61. #include <GL/glut.h>
  62.  
  63. static int stencilOn = 1;
  64.  
  65. /* function declarations */
  66.  
  67. void
  68.   drawScene(void), setMatrix(void), animation(void), resize(int w, int h),
  69.   keyboard(unsigned char c, int x, int y), menu(int choice), drawWireframe(int face),
  70.   drawFilled(int face);
  71.  
  72. /* global variables */
  73.  
  74. float ax, ay, az;       /* angles for animation */
  75.  
  76. /* coords of a cube */
  77. static GLfloat cube[8][3] =
  78. { { 0.0, 0.0, 0.0},
  79.   { 1.0, 0.0, 0.0},
  80.   { 1.0, 0.0, 1.0},
  81.   { 0.0, 0.0, 1.0},
  82.   { 1.0, 1.0, 0.0},
  83.   { 1.0, 1.0, 1.0},
  84.   { 0.0, 1.0, 1.0},
  85.   { 0.0, 1.0, 0.0}};
  86.  
  87. static int faceIndex[6][4] =
  88. {{0, 1, 2, 3},
  89.  {1, 4, 5, 2},
  90.  {4, 7, 6, 5},
  91.  {7, 0, 3, 6},
  92.  {3, 2, 5, 6},
  93.  {7, 4, 1, 0}};
  94. int
  95. main(int argc, char **argv)
  96. {
  97.   glutInit(&argc, argv);
  98.  
  99.   glutInitWindowSize(400, 400);
  100.   glutInitDisplayMode(GLUT_RGB | GLUT_STENCIL | GLUT_DOUBLE | GLUT_DEPTH);
  101.   glutCreateWindow("Stenciled hidden surface removal");
  102.  
  103.   ax = 10.0;
  104.   ay = -10.0;
  105.   az = 0.0;
  106.  
  107.   glutDisplayFunc(drawScene);
  108.   glutReshapeFunc(resize);
  109.   glutCreateMenu(menu);
  110.   glutAddMenuEntry("Motion", 3);
  111.   glutAddMenuEntry("Stencil on", 1);
  112.   glutAddMenuEntry("Stencil off", 2);
  113.   glutAttachMenu(GLUT_RIGHT_BUTTON);
  114.   glutKeyboardFunc(keyboard);
  115.   glutMainLoop();
  116.   return 0;             /* ANSI C requires main to return int. */
  117. }
  118.  
  119. void
  120. drawWireframe(int face)
  121. {
  122.   int i;
  123.   glBegin(GL_LINE_LOOP);
  124.   for (i = 0; i < 4; i++)
  125.     glVertex3fv((GLfloat *) cube[faceIndex[face][i]]);
  126.   glEnd();
  127. }
  128.  
  129. void
  130. drawFilled(int face)
  131. {
  132.   int i;
  133.   glBegin(GL_POLYGON);
  134.   for (i = 0; i < 4; i++)
  135.     glVertex3fv((GLfloat *) cube[faceIndex[face][i]]);
  136.   glEnd();
  137. }
  138.  
  139. void
  140. drawScene(void)
  141. {
  142.  
  143.   int i;
  144.   glEnable(GL_DEPTH_TEST);
  145.   glDepthFunc(GL_LEQUAL);
  146.  
  147.   glClearColor(0.0, 0.0, 0.0, 0.0);
  148.   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  149.  
  150.   glPushMatrix();
  151.  
  152.   glRotatef(ax, 1.0, 0.0, 0.0);
  153.   glRotatef(-ay, 0.0, 1.0, 0.0);
  154.  
  155.   /* all the good stuff follows */
  156.  
  157.   if (stencilOn) {
  158.     glEnable(GL_STENCIL_TEST);
  159.     glClear(GL_STENCIL_BUFFER_BIT);
  160.     glStencilMask(1);
  161.     glStencilFunc(GL_ALWAYS, 0, 1);
  162.     glStencilOp(GL_INVERT, GL_INVERT, GL_INVERT);
  163.   }
  164.   glColor3f(1.0, 1.0, 0.0);
  165.   for (i = 0; i < 6; i++) {
  166.     drawWireframe(i);
  167.     if (stencilOn) {
  168.       glStencilFunc(GL_EQUAL, 0, 1);
  169.       glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
  170.     }
  171.     glColor3f(0.0, 0.0, 0.0);
  172.     drawFilled(i);
  173.  
  174.     glColor3f(1.0, 1.0, 0.0);
  175.     if (stencilOn) {
  176.       glStencilFunc(GL_ALWAYS, 0, 1);
  177.       glStencilOp(GL_INVERT, GL_INVERT, GL_INVERT);
  178.     }
  179.     glColor3f(1.0, 1.0, 0.0);
  180.     drawWireframe(i);
  181.   }
  182.   glPopMatrix();
  183.  
  184.   if (stencilOn)
  185.     glDisable(GL_STENCIL_TEST);
  186.  
  187.   /* end of good stuff */
  188.  
  189.   glutSwapBuffers();
  190. }
  191.  
  192. void
  193. setMatrix(void)
  194. {
  195.   glMatrixMode(GL_PROJECTION);
  196.   glLoadIdentity();
  197.   glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0);
  198.   glMatrixMode(GL_MODELVIEW);
  199.   glLoadIdentity();
  200. }
  201.  
  202. int count = 0;
  203.  
  204. void
  205. animation(void)
  206. {
  207.   /* animate the cone */
  208.  
  209.   ax += 5.0;
  210.   ay -= 2.0;
  211.   az += 5.0;
  212.   if (ax >= 360)
  213.     ax = 0.0;
  214.   if (ay <= -360)
  215.     ay = 0.0;
  216.   if (az >= 360)
  217.     az = 0.0;
  218.   glutPostRedisplay();
  219.   count++;
  220.   if (count >= 60)
  221.     glutIdleFunc(NULL);
  222. }
  223.  
  224. void
  225. menu(int choice)
  226. {
  227.   switch (choice) {
  228.   case 3:
  229.     count = 0;
  230.     glutIdleFunc(animation);
  231.     break;
  232.   case 2:
  233.     stencilOn = 0;
  234.     glutSetWindowTitle("Stencil Disabled");
  235.     glutPostRedisplay();
  236.     break;
  237.   case 1:
  238.     stencilOn = 1;
  239.     glutSetWindowTitle("Stencil Enabled");
  240.     glutPostRedisplay();
  241.     break;
  242.   }
  243. }
  244.  
  245. /* ARGSUSED1 */
  246. void
  247. keyboard(unsigned char c, int x, int y)
  248. {
  249.   switch (c) {
  250.   case 27:
  251.     exit(0);
  252.     break;
  253.   default:
  254.     break;
  255.   }
  256. }
  257.  
  258. void
  259. resize(int w, int h)
  260. {
  261.   glViewport(0, 0, w, h);
  262.   setMatrix();
  263. }
  264.